package com.jdz.gxq.info.admin.training.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import com.jdz.gxq.info.admin.training.bo.Training;
import com.jdz.gxq.info.admin.training.form.EditTrainingForm;
import com.jdz.gxq.info.admin.training.service.TrainingService;
import com.jdz.gxq.info.common.action.CommonAction;

public class SaveTrainingAction extends CommonAction {

	private TrainingService trainingService = null;
	 
	public ActionForward doExecute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws java.lang.Exception {
		
		EditTrainingForm editTrainingForm = (EditTrainingForm) form;
		
		Training training = editTrainingForm.getTraining();
		
		// training名重复验证(其他后台逻辑验证也类似处理)
		if (!trainingService.checkTrainingName(training, 2)) {
			ActionMessages messages = new ActionMessages();
			messages.add("training.enterpriseName", new ActionMessage("error.trainingNameExisted"));
			addErrors(request, messages);
			return mapping.getInputForward();
		}

		training.setModifiedBy(getUserId(request));
		
		trainingService.updateTraining(training);
		
		return mapping.findForward("success");
	}

	public void setTrainingService(TrainingService trainingService) {
		this.trainingService = trainingService;
	}
}

